﻿using ArcyunNew.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using BLL;
using System.Linq;

namespace ArcyunNew.Controllers
{
    public class AppController : Controller
    {
        public ActionResult SmartStore()
        {
            string StoreLink_IOS_DownURL = ConfigurationManager.AppSettings["StoreLink_IOSDownURL"];
            string StoreLink_Android_DownURL = ConfigurationManager.AppSettings["StoreLink_AndroidDownURL"];

            DownHtmlReturn requestModel = new DownHtmlReturn();
            requestModel.IOSUrl = StoreLink_IOS_DownURL.ToString();
            requestModel.AndroidUrl = StoreLink_Android_DownURL.ToString();
            requestModel.StrSystem = "在浏览器中打开";
            requestModel.StrImgAndroid = "../../Content/Images/App/SmartStore/wap-android.jpg";
            requestModel.StrImgIOS = "../../Content/Images/App/SmartStore/wap-apple.jpg";

            DownHtmlReturn model = GetDownHtml(requestModel);

            ViewData["model"] = model;
            return View();
        }

        /// <summary>
        /// 试装镜
        /// </summary>
        /// <returns></returns>
        public ActionResult Szj()
        {
            return View();
        }
        /// <summary>
        /// 爱美派
        /// </summary>
        /// <returns></returns>
        public ActionResult Amp()
        {
            string DownURL = ""; //下载地址
            string ProductKeyID = "";
            string bossid = "";
            string s = "";
            string source = "";
            string f = "";
            string version = "";
            string device = "";
            string showname = "爱美派";//商家名称
            //string imgURL = "";//logo

            string activitycode = "";//邀约有礼，活动对外编号
            string uid = "";//邀约有礼，推荐人编号，对应AMP_Invitefriends表里面的CustomerID

            bossid = Request["g"] == null ? "" : Request["g"].ToString();
            s = Request["s"] == null ? "" : Request["s"].ToString();
            source = Request["source"] == null ? "" : Common.StringHelper.UrlDecode(Request["source"].ToString());
            f = Request["f"] == null ? "" : Common.StringHelper.UrlDecode(Request["f"].ToString());
            version = Request["version"] == null ? "" : Common.StringHelper.UrlDecode(Request["version"].ToString());
            device = Request["device"] == null ? "" : Common.StringHelper.UrlDecode(Request["device"].ToString());

            //以下两个参数由邀约有礼使用
            activitycode = Request["activitycode"] == null ? "" : Common.StringHelper.UrlDecode(Request["activitycode"].ToString());
            uid = Request["uid"] == null ? "" : Common.StringHelper.UrlDecode(Request["uid"].ToString());

            
            if (bossid == "" && s.Length >= 6)
            {
                bossid = s.Substring(0, 6);
            }
                    
            //获取商家信息
            //GetData(bossid);
            int i;
            string brandname = "";
            string companyname = "";
            i = BLL.SOP_ProductKeyBLL.GetSOP_ProductKeyName(bossid, out brandname, out companyname);

            if (i > 0)
            {
                ProductKeyID = i.ToString();
                showname = brandname == "" ? companyname : brandname;
            }

            //获取下载地址
            DownURL = GetInstallURL(bossid);

            //如果是莫施（900000）和嘉媚乐（900004），则直接跳到下载页面，无需注册
            string amp_NoNeedRegister_bossid = ConfigurationManager.AppSettings["amp_NoNeedRegister_bossid"] == null ? "" : ConfigurationManager.AppSettings["amp_NoNeedRegister_bossid"];
            if (amp_NoNeedRegister_bossid.IndexOf(bossid) > -1)
            {
                Response.Redirect(DownURL);
            }

            ViewBag.ProductKeyID = ProductKeyID;
            ViewBag.BossID = bossid;
            ViewBag.S = s;
            ViewBag.source = source;
            ViewBag.f = f;
            ViewBag.version = version;
            ViewBag.device = device;
            ViewBag.uid = uid;
            ViewBag.activitycode = activitycode;
            ViewBag.DownURL = DownURL;
            return View();
            
        }

        /// <summary>
        /// 冠军美顾
        /// </summary>
        /// <returns></returns>
        public ActionResult Ba()
        {
            string BA_IOS_DownURL = "", BA_Android_DownURL = "";
            BA_IOS_DownURL = ConfigurationManager.AppSettings["BA_IOSDownURL"];
            BA_Android_DownURL = ConfigurationManager.AppSettings["BA_AndroidDownURL"];

            DownHtmlReturn requestModel = new DownHtmlReturn();
            requestModel.IOSUrl = BA_IOS_DownURL.ToString();
            requestModel.AndroidUrl = BA_Android_DownURL.ToString();
            requestModel.StrSystem = "在浏览器中打开";
            requestModel.StrImgAndroid = "../../Content/Images/App/Ba/wap-android.jpg";
            requestModel.StrImgIOS = "../../Content/Images/App/Ba/wap-apple.jpg";

            DownHtmlReturn model = GetDownHtml(requestModel);

            ViewData["model"] = model;
            return View();
        }

        /// <summary>
        /// 管理助手
        /// </summary>
        /// <returns></returns>
        public ActionResult Boss()
        {
            string strTips_Android = "<p class=\"tips\">仅支持Android4.0以上的系统版本</p>";
            string strTips_IOS = "<p class=\"tips\">仅支持IOS6.0以上的系统版本</p>";
            string strTips = "<p class=\"tips\">仅支持Android4.0及IOS6.0以上的系统版本</p>";
            string Boss_IOS_DownURL = "", Boss_Android_DownURL = "";
            Boss_IOS_DownURL = ConfigurationManager.AppSettings["Boss_IOSDownURL"];
            Boss_Android_DownURL = ConfigurationManager.AppSettings["Boss_AndroidDownURL"];

            DownHtmlReturn requestModel = new DownHtmlReturn();
            requestModel.IOSUrl = Boss_IOS_DownURL.ToString();
            requestModel.AndroidUrl = Boss_Android_DownURL.ToString();
            requestModel.StrSystem = "在浏览器中打开";
            requestModel.StrImgAndroid = "../../Content/Images/App/Boss/bg-android.jpg";
            requestModel.StrImgIOS = "../../Content/Images/App/Boss/bg-ios.jpg";

            DownHtmlReturn model = GetDownHtml(requestModel);

            //添加说明
            if (model.userAgent.IndexOf("IOS") > -1)
            {
                model.StrDownHtml += strTips_IOS;
            }
            else if (model.userAgent.IndexOf("android") > -1)
            {
                model.StrDownHtml += strTips_Android;
            }
            else
            {
                model.StrDownHtml += strTips;
            }
            ViewData["model"] = model;
            return View();
        }

        /// <summary>
        /// 获取App下载地址链接
        /// </summary>
        /// <param name="IOSUrl"></param>
        /// <param name="AndroidUrl"></param>
        /// <returns></returns>
        private DownHtmlReturn GetDownHtml(DownHtmlReturn requestStr)
        {
            DownHtmlReturn model = requestStr;

            string IsUserAgent = "";

            UAParserUserAgent userAgent = new UAParserUserAgent(this.HttpContext);
            string raw = userAgent.RawValue.ToString().ToLower();

            model.userAgent = raw;
            if (raw.IndexOf("micromessenger") != -1)
            {
                model.IsUserAgent = "weixin";
            }
            else if (raw.IndexOf("weibo") != -1)
            {
                model.IsUserAgent = "weibo";
            }
            else
            {
                model.IsUserAgent = "";
            }
            if (raw.IndexOf("iphone") > -1 || raw.IndexOf("itouch") > -1)
            {
                model.StrImg = model.StrImgIOS;
                if (IsUserAgent != "")
                {
                    model.StrDownHtml = "<a href=\"javascript:void(0);\" onclick=\"IOSDownload();\" class=\"a_btn_i\"><img src=\"../../Content/Images/App/Ba/icon_Apple.png\" style=\" width:23px; height:26px; margin-bottom: -4px; margin-right:12px;\" />立即安装</a>";
                }
                else
                {
                    model.StrDownHtml = "<a href=\"" + requestStr.IOSUrl + "\" class=\"a_btn_apple\"><img src=\"../../Content/Images/App/Ba/icon_Apple.png\" style=\" width:23px; height:26px; margin-bottom: -4px; margin-right:12px;\" />立即安装</a>";
                }
                model.StrSystem = "在Safari中打开";
            }
            else if (raw.IndexOf("android") > -1)
            {
                model.StrImg = model.StrImgAndroid;
                if (IsUserAgent == "weixin")
                {
                    model.StrDownHtml = "<a href=\"javascript:void(0);\" onclick=\"AndoridDownload();\" class=\"a_btn_a\"><img src=\"../../Content/Images/App/Ba/icon_Android.png\" style=\" width:24px; height:30px; margin-bottom: -7px; margin-right:15px;\" />立即安装</a>";
                }
                else
                {
                    model.StrDownHtml = "<a href=\"" + requestStr.AndroidUrl + "\" class=\"a_btn_a\"><img src=\"../../Content/Images/App/Ba/icon_Android.png\" style=\" width:24px; height:30px; margin-bottom: -7px; margin-right:15px;\" />立即安装</a>";
                }
                model.StrSystem = "在浏览器中打开";
            }
            else
            {
                model.StrImg = model.StrImgIOS;
                model.StrSystem = "在浏览器中打开";
                if (IsUserAgent != "")
                {
                    model.StrDownHtml = "<a href=\"javascript:void(0);\" onclick=\"IOSDownload();\" class=\"a_btn_apple\"><img src=\"../../Content/Images/App/Ba/icon_Apple.png\" style=\" width:19px; height:22px; margin-bottom: -4px; margin-right:10px;\" />立即安装</a>";
                    model.StrDownHtml += "<a  href=\"javascript:void(0);\" onclick=\"AndoridDownload();\" class=\"a_btn_android\" id=\"ba_downurl\"><img src=\"../../Content/Images/App/Ba/icon_Android.png\" style=\" width:18px; height:23px; margin-bottom: -5px; margin-right:10px;\" />立即安装</a>";
                }
                else
                {
                    model.StrDownHtml = "<a href=\"" + requestStr.IOSUrl + "\" class=\"a_btn_apple\"><img src=\"images/icon_Apple.png\" style=\" width:19px; height:22px; margin-bottom: -4px; margin-right:10px;\" />立即安装</a>";
                    model.StrDownHtml += "<a  href=\"" + requestStr.AndroidUrl + "\" class=\"a_btn_android\" id=\"ba_downurl\"><img src=\"../../Content/Images/App/Ba/icon_Android.png\" style=\" width:18px; height:23px; margin-bottom: -5px; margin-right:10px;\" />立即安装</a>";
                }
            }
            return model;
        }

        protected string  GetInstallURL(string BossID)
        {
            string DownURL = "";
            try
            {
                string AMP_AndroidDownURL = ""; //Android下载地址
                string AMP_IOSDownURL = ""; //IOS下载地址

                //从数据库获取下载地址
                IList<Model.SOP_AppInstall> AppInstallList = new List<Model.SOP_AppInstall>();
                AppInstallList = SOP_AppInstallBLL.GetAppinstallList(BossID, "");
                if (AppInstallList != null && AppInstallList.Count > 0)
                {
                    var iosquery = AppInstallList.Where(c => c.AppID == 32010).ToList().First();
                    AMP_IOSDownURL = iosquery.AppInstallURL;

                    var androidquery = AppInstallList.Where(c => c.AppID == 31010).ToList().First();
                    AMP_AndroidDownURL = androidquery.AppInstallURL;
                }

                //判断手机平台，给出不同下载地址。
                UAParserUserAgent userAgent = new UAParserUserAgent(this.HttpContext);
                string raw = userAgent.RawValue.ToString().ToLower();
                if (raw.Contains("iphone") || raw.Contains("itouch"))
                {
                    DownURL = AMP_IOSDownURL;
                }
                else
                {
                    DownURL = AMP_AndroidDownURL;
                }
                return DownURL;

            }
            catch (Exception ex)
            {
                ActCommon.Log.WriteLog("ampinstall(date:" + System.DateTime.Now.ToString() + ")error:" + ex.Message + ";stackTrace" + ex.StackTrace, "install");
                return DownURL;
            }
        }
    }

    public class DownHtmlReturn
    {
        public string StrImg { get; set; }
        public string StrImgIOS { get; set; }
        public string StrImgAndroid { get; set; }

        /// <summary>
        /// 文本
        /// </summary>
        public string StrSystem { get; set; }

        public string StrDownHtml { get; set; }

        public string IsUserAgent { get; set; }
        public string IOSUrl { get; set; }
        public string AndroidUrl { get; set; }
        public string userAgent { get; set; }
    }
}